using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using AvansRS.Domain;
using AvansRS.Domain.DataInterface;
using AvansRS.Data;
using AvansRS.Web.Util;
using StructureMap;

namespace AvansRS.Web.Controllers
{
	public class UserController : Controller
	{
		IPersonRepository _personRepository;
		IAccountRepository _accountRepository;
        IRepository<Academy> mAcademyRepository;
        IRepository<Study> mStudyRepository;
	    private IRepository<Entity> _entityRepository;

		public UserController(IPersonRepository personRepository, IAccountRepository accountRepository,
            IRepository<Academy> academyRepository, IRepository<Study> studyRepository, IRepository<Entity> entityRepository)
		{
			_personRepository = ObjectFactory.GetInstance<IPersonRepository>();
			_accountRepository = ObjectFactory.GetInstance<IAccountRepository>();
            mAcademyRepository = academyRepository;
            mStudyRepository = studyRepository;
		    _entityRepository = entityRepository;
		}

		public ActionResult Index(string type)
		{
			IList<Person> persons = _personRepository.FindByPersonType(type);
			Session["type"] = type;
			if(TempData["ModelState"] as ModelStateDictionary != null)
			{
				ModelState.Merge(TempData["ModelState"] as ModelStateDictionary);
			}
			return View(persons);
		}

		//
		// GET: /Person/Details/5

		public ActionResult Details(int id)
		{
			Person person = _personRepository.GetByID(id);
			return View(person);
		}

		//
		// GET: /Person/Create

		public ActionResult Create()
		{
			return View();
		}

		//
		// POST: /Person/Create

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Create([Bind(Exclude = "ID")] Person person,
											[Bind(Exclude = "ID")] Account account,
											Password password)
		{
			if(!ModelState.IsValid)
				return View();

			account.Password = password.InitialPassword;

			_accountRepository.Add(account);

            Entity entity = new Entity();
            _entityRepository.Add(entity);

		    person.Entity = entity;
			person.Account = account;

			_personRepository.Add(person);


			return RedirectToAction("Index", "User", new { type = Session["type"] });
		}

		//
		// GET: /Person/Edit/5

		public ActionResult Edit(int id)
		{
			Person person = _personRepository.GetByID(id);
            SelectList academyList = new SelectList(mAcademyRepository.GetAll(), "ID", "Name");
            ViewData["academylist"] = academyList;
            SelectList studylist = new SelectList(mStudyRepository.GetAll(), "ID", "Name");
            ViewData["studylist"] = studylist;
			return View(person);
		}

        public ActionResult Profile()
        {
            SelectList academyList = new SelectList(mAcademyRepository.GetAll(), "ID", "Name");
            ViewData["academylist"] = academyList;
            SelectList studylist = new SelectList(mStudyRepository.GetAll(), "ID", "Name");
            ViewData["studylist"] = studylist;

            Person person = _personRepository.FindByAccountUsername(User.Identity.Name);

            return View("Edit", person);
        }

		//
		// POST: /Person/Edit/5

		[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(Person person, Account account, Password password, int academyID, int studyID)
		{
			string newPassword;
			if(password.IsEmpty())
			{
				ModelState["InitialPassword"].Errors.Clear();
				ModelState["ConfirmPassword"].Errors.Clear();
				newPassword = _accountRepository.GetByID(account.ID).Password;
			}
			else
			{
				newPassword = password.InitialPassword;
			}

            if (!ModelState.IsValid)
            {
                SelectList academyList = new SelectList(mAcademyRepository.GetAll(), "ID", "Name");
                ViewData["academylist"] = academyList;
                SelectList studylist = new SelectList(mStudyRepository.GetAll(), "ID", "Name");
                ViewData["studylist"] = studylist;
                return View(_personRepository.GetByID(person.ID));
            }

		    Entity entity = _personRepository.GetByID(person.ID).Entity;
		    person.Entity = entity;


            Study study = mStudyRepository.GetByID(studyID);
            person.Study = study;

            Academy academy = mAcademyRepository.GetByID(academyID);
            person.Academy = academy;

			account.Password = newPassword;
			person.Account = account;

            mAcademyRepository.Save(academy);
            mStudyRepository.Save(study);
			_personRepository.Save(person);
			_accountRepository.Save(account);

			return RedirectToAction("Index", "User", new { type = Session["type"] });
		}
	}
}
